package com.init.engineering.service.impl;

import com.init.engineering.dao.ProjectDao;
import com.init.engineering.pages.PageResult;
import com.init.engineering.pages.QueryPageBean;
import com.init.engineering.pojo.Project;
import com.init.engineering.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired(required = false)
    private ProjectDao projectDao;
    /**
     * 查询所有工程信息
     * @return
     */
    @Override
    public List<Project> findProjects() {
        System.out.println("检查projcetDao是否注入成功" + projectDao);
        List<Project> projects = projectDao.findProjects();
        return projects;
    }

    /**
     * 分页条件查询，将结果封装到PageResult内
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findProjectsByPage(QueryPageBean queryPageBean) {
        /**
         * 获取当前页的第一条数据在数据库内的起始位置
         * 公式:
         *   当前页起始位置 = (当前页 - 起始页(1)) * 分页显示最大条目数
         */
        Integer begin = (queryPageBean.getCurrentPage() - 1) * queryPageBean.getPageSize();
        queryPageBean.setCurrentPage(begin);
        //查询当前页的数据集合
        List<Project> projects = projectDao.findProjectsByPage(queryPageBean);
        //获取分页查询的条件
        String queryString = queryPageBean.getQueryString();
        //获取分页查询按条件查询的总条数
        long totalCount = projectDao.selectTotalCount(queryString);

        //将数据封装到PageResult内
        PageResult pageResult = new PageResult();
        pageResult.setTotal(totalCount);
        pageResult.setRows(projects);
        return pageResult;
    }

    /**
     * 新增工程
     * @param project
     * @return
     */
    @Override
    @Transactional
    public int saveProject(Project project) {
        int row = projectDao.saveProject(project);
        return row;
    }

    /**
     * 通过pid删除一个工程
     * @param pid
     * @return
     */
    @Override
    public int deleteProject(int pid) {
        int row = projectDao.updateStatusProject(pid);
        return row;
    }

    /**
     * 通过工程id找到唯一工程
     * @param pid
     * @return
     */
    @Override
    public Project findProjectByPid(int pid) {
        Project project = projectDao.findProjectByPid(pid);
        return project;
    }

    /**
     * 更新工程信息
     * @param project
     * @return
     */
    @Override
    public int updateProject(Project project) {
        int row = projectDao.updateProject(project);
        return row;
    }

    /**
     * 通过开发商did找到对应的工程
     * @param did
     * @return
     */
    @Override
    public List<Project> findProjectsByDid(String did) {
        List<Project> projects = projectDao.findProjectsByDid(did);
        return projects;
    }

    /**
     * 通过承包商cid找到对应的工程
     * @param cid
     * @return
     */
    @Override
    public List<Project> findProjectsByCid(String cid) {
        List<Project> projects = projectDao.findProjectsByCid(cid);
        return projects;
    }
}
